#include "add_atv.h"
#include "ui_add_atv.h"

Add_ATV::Add_ATV(int num1,int num2,QList<uint> *atv,QMap<uint,uchar> *dtv,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Add_ATV)
{
    atv_num=atv;
    dtv_num=dtv;
    solution=num1;
    ch_val=num2;

    ui->setupUi(this);
    My_Init();
}

Add_ATV::~Add_ATV()
{
    delete ui;
}
int Add_ATV::Check_Num(int num)
{
    for(int i=0;i<atv_num->size();i++)
    {
        if(num==atv_num->at(i))
        {
            QMessageBox::warning(this,"Error","The Channel Number is repeated with atv data!");
            return -1;
        }
    }
    QMap<uint,uchar>::iterator iter;
    for(iter=dtv_num->begin();iter!=dtv_num->end();iter++)
    {
        if(iter.key()==num)
        {
            QMessageBox::warning(this,"Error","The Channel Number is repeated whih dtv data");
            return -1;
        }
    }
    return 1;
}
void Add_ATV::Check_Num()
{
    CHECK_INPUT_EMPTY(ui->Item1->text());
    ui->msg->setText("");
    bool Flag=false;
    bool *ok=&Flag;
    uint num=ui->Item1->text().toUInt(ok);
    if(*ok==false)
    {
        ui->msg->setText("Channel Numbet Error");
        ui->pB_Confirm->setEnabled(false);
        return ;
    }else{
        ui->pB_Confirm->setEnabled(true);
    }
    for(int i=0;i<atv_num->size();i++)
    {
        if(num==atv_num->at(i))
        {
          //  QMessageBox::warning(this,"Error","The Channel Number is repeated with atv data!");
            ui->msg->setText("The channel number is repeated with atv data!");
            ui->pB_Confirm->setEnabled(false);
            return ;
        }
    }
    QMap<uint,uchar>::iterator iter;
    for(iter=dtv_num->begin();iter!=dtv_num->end();iter++)
    {
        if(iter.key()==num)
        {
            //QMessageBox::warning(this,"Error","The Channel Number is repeated whih dtv data");
            ui->msg->setText("The channel number is repeated whih dtv data");
            ui->pB_Confirm->setEnabled(false);
            return ;
        }
    }
    ui->pB_Confirm->setEnabled(true);
}

void Add_ATV::Check_Freq()
{
    QString text=ui->Item4->text();
    bool Flag=false;
    bool *ok=&Flag;
    text.toInt(ok);
    if(*ok==false)
    {
        ui->msg->setText("The Freqency Error");
        ui->pB_Confirm->setEnabled(false);
        return ;
    }else
    {
        ui->msg->setText("");
        ui->pB_Confirm->setEnabled(true);
    }
}

void Add_ATV::My_Init()
{
    //this->setm
    ui->msg->setStyleSheet("color:red");
    this->setStyleSheet("background-color:rgb(135,206,235)");
    ui->Item1->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item2->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item3->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item4->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item5->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item6->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->label->setText("Channel Number:");
    if(solution==1)
    {
        ui->Item1->setText(QString::number(ch_val));
        ui->Item1->setEnabled(false);
    }
    ui->label_2->setText("Channel Name:");
    ui->label_3->setText("Channel Class:");
    QStringList tmp;
    tmp.append("air");
    tmp.append("cable");
    ui->Item3->addItems(tmp);

    ui->label_4->setText("Channel Freq:");
    ui->label_5->setText("Channel Favorite:");
    tmp.clear();
    tmp.append("0");
    tmp.append("1");
    ui->Item5->addItems(tmp);
    ui->label_6->setText("Channel Skip:");
    ui->Item6->addItems(tmp);
    connect(ui->Item1,SIGNAL(editingFinished()),this,SLOT(Check_Num()));
    connect(ui->pB_Confirm,SIGNAL(clicked()),this,SLOT(Confirm()));
    connect(ui->Item4,SIGNAL(editingFinished()),this,SLOT(Check_Freq()));
}

void Add_ATV::Confirm()
{
    QList<QString>Item_Data;
    QString tmp="";
    if(1==solution)
        tmp=QString::number(ch_val);
    else
    {
       // bool Flag=false;
        //bool *ok=&Flag;
        //int num=ui->Item1->text().toUInt(ok);

        //STRING_TRAN_ERROR_0;
        /*if(*ok==false)
        {
            ui->msg->setText("The Channel Number Error");
            return ;
        }
        if(-1==Check_Num(num))
        {
            ui->Item1->clear();
            ui->Item2->clear();
            return ;
        }*/
        tmp=ui->Item1->text();
    }
    CHECK_INPUT_EMPTY(tmp);
    Item_Data.append(tmp);
    tmp=ui->Item2->text();
    CHECK_INPUT_EMPTY(tmp)
    Item_Data.append(tmp);
    tmp=QString::number(ui->Item3->currentIndex());
    CHECK_INPUT_EMPTY(tmp);
    Item_Data.append(tmp);
    tmp=ui->Item4->text();
    if(tmp.toInt()<=0)
    {
        ui->msg->setText("Frequency error");
        return ;
    }
    Item_Data.append(tmp);
    tmp=QString::number(ui->Item5->currentIndex());
    Item_Data.append(tmp);
    CHECK_INPUT_EMPTY(tmp);
    tmp=QString::number(ui->Item6->currentIndex());
    CHECK_INPUT_EMPTY(tmp);
    Item_Data.append(tmp);

    if(0==solution)
    {
        TCL_MTK_ATV_TV *mtk_atv=new TCL_MTK_ATV_TV;
        mtk_atv->Set_Data(Item_Data);
        emit Add_MTK_ATV(mtk_atv);

    }else if(1==solution)
    {
        TCL_MSTAR_ATV_TV *mstar_atv=new TCL_MSTAR_ATV_TV;
        mstar_atv->Set_Data(Item_Data);
        emit Add_MSTAR_ATV(mstar_atv);
    }else{
        //QMessageBox::warning(this,"Warning","Solution error");
        ui->msg->setText("Failed");
        return ;
    }
    //connect(MainWindow,SIGNAL(Add_SUCCESS(bool)),this,SLOT(Add_SUCCESS(bool)));
    if(solution==1)
    {
        ui->Item1->setText(QString::number(ch_val+1));
        ch_val=ch_val+1;
    }

    ui->msg->setText("Success");
    ui->Item1->setText("");
    ui->Item2->setText("");

}
